<template>
  <el-card class="box-card">
    <h2>管理员登录日志</h2>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" stripe highlight-current-row border>
      <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
      <el-table-column prop="username" label="用户" width="150" align="center"></el-table-column>
      <el-table-column prop="log_Content" label="日志内容" width="150" align="center"></el-table-column>
      <el-table-column prop="ip_Address" label="IP地址" width="150" align="center"></el-table-column>
      <el-table-column prop="os" label="操作系统名称" width="200" align="center"></el-table-column>
      <el-table-column prop="ie" label="浏览器" width="200" align="center"></el-table-column>
      <el-table-column prop="createDate" label="登录日期" width="200" align="center" :formatter="formatDate"></el-table-column>
      <el-table-column prop="remark" label="登录状况" width="150" align="center"></el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 4, 5, 10]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  data() {
    return {
     tableData: [],
     pageInfo: { total: 0 },
     pageSize: 10,
     currentPage: 1
   };
  },
  methods: {
    formatDate(row, column, cellValue) {
     if (!cellValue) return "";
      const date = new Date(cellValue);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.fetchLogData();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.fetchLogData();
    },
    async fetchLogData() {
      try {
        const response = await axios.get("/log/logs");
        this.tableData = response.data;
        this.pageInfo.total = response.data.length;
     } catch (error) {
        console.error('获取登录日志失败:', error);
        ElMessage.error('获取登录日志失败');
      }
    }
  },
  mounted() {
    this.fetchLogData();
  }
};
</script>

<style scoped>
.box-card {
  margin: 20px;
}
</style>